define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
		$scope.form = {};
		var currentNode = null;
		var menuTreeObj;
		var tId = "";

		//单击
		var zTreeOnClick = function(event, treeId, treeNode) {
			$scope.dept_name = treeNode.name;
			currentNode = treeNode;
			tId = currentNode.tId;

			$scope.findrole(treeNode);
			$scope.$apply();
		};
		// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			check: {
				enable: false
			},
			edit: {
				enable: true
			},
			view:{
				showIcon: false,
				selectedMulti: false
			},
			callback: {
				onClick: zTreeOnClick
			}
		};
		//学校部门
		$scope.findrolegroup = function(){
			var zNodes =  [];
			httpService.post(app.urls.role_group_findGroupTree, {}, function(data){
				if("0000" === data.code) {
					zNodes = data.data;
					menuTreeObj = $.fn.zTree.init($("#groupTree"), setting, zNodes);
					$scope.$apply();

					var nodes = menuTreeObj.getNodes();
					if (nodes.length > 0) {
						currentNode = nodes[0];
						menuTreeObj.selectNode(nodes[0]);

						menuTreeObj.expandAll(true);
						setting.callback.onClick(null, menuTreeObj.setting.treeId, nodes[0]);
					}

				}else{
					eventBusService.publish("modal.alert", {"content": data.msg});
				}
			});
		}
		$scope.findrolegroup();

	$scope.dataInfo = {};
	$scope.findunit = function(){
		httpService.post(app.urls.role_group_selectByType,$scope.form,function(data){
			if("0000" === data.code){
				httpService.post(app.urls.unit_selectType, $scope.form, function(data) {
					if ("0000" === data.code) {
						if(data.data.type == '1' || data.data.type == '2'){
							$scope.form.fk_unit='self';
						}
						$scope.findunitlist();
					} else {
						alert(data.msg);
					}
				});
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.findunit();
	
	$scope.findunitlist = function(){
		// 查询所有单位
		httpService.post(app.urls.unit_selectUnit,$scope.form,function(data){
			if("0000" === data.code){
				$scope.unitList = data.data;
				if($scope.unitList.length>1){$scope.form.attach=true;}else{$scope.form.attach=false;}
				$scope.findusertype();
			}else{
				alert(data.msg);
			}
		});
	}

	$scope.findusertype = function(){
		httpService.post(app.urls.role_selectType,{},function(data){
			if("0000" === data.code){
				$scope.maxtype = data.data[0].type;
				
				var ids="";
				
				if($scope.maxtype == '0'){
					ids = ",'0','1','2','3'";
					$('.schoolList').show();
				}else if($scope.maxtype == '1'){
					ids = ",'1','2','3'";
					$('.schoolList').show();
				}else if($scope.maxtype == '2'){
					ids = ",'2','3'";
					$('.schoolList').hide();
				}else if($scope.maxtype == '3'){
					ids = ",'3'";
					$('.schoolList').hide();
				}
				
				$scope.form.ids = ids.replace(',','');
				
				$scope.findrolegroup(0);
			}else{
				alert(data.msg);
			}
		});
	}

		$scope.findrole = function(row){
			
			$scope.groupt = row;
			
			$scope.form.fk_group=row.id;
			
			httpService.post(app.urls.role_selectByType,$scope.form,function(data){
				if("0000" === data.code){
					$scope.roles = data.data;
					$scope.checkMenu($scope.roles[0],$scope.roles[0].type);
				}else{
					alert(data.msg);
				}
			});
		}
		

		// 获取具体用户数据
		$scope.checkMenu = function(row, type) {
			$scope.show = "true";
			$scope.fk_role = row.id;
			$scope.form.fk_role = row.id;
			$scope.row_name = row.name;
			$scope.fk_group = row.fk_group;
			$scope.type = type;
			if($scope.fk_group==undefined){
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:none;");
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:none;");
					$scope.form.unitshow = false;
				}else{
				if(type == '0'){
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:none;");
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:;");
					$scope.form.unitshow = false;
				}else if(type == '3'){
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:");
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:none;");
					$scope.form.unitshow = true;
				}else{
					$("#"+$scope.controllerName + "_user_tea").attr("style","display:");
					$("#"+$scope.controllerName + "_user_stu").attr("style","display:none;");
					$scope.form.unitshow = false;
				}
			}
			$scope.find();
		}

		$scope.find = function() {
			if($scope.dataInfo.fk_unit != null) {
				$scope.form.fk_unit = $scope.dataInfo.fk_unit;
			}
			httpService.postpage(app.urls.role_selectUserRole,{"id":$scope.form.fk_role,"fk_unit":$scope.form.fk_unit,"id_crad":$scope.form.id_crad,"name":$scope.form.name},function(data){
				if("0000" === data.code){
					$scope.rows = data.data;
				}else{
					alert(data.msg);
				}
			});
		}
	
	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.find();
	});
	//接收数据刷新
	eventBusService.subscribe('content.pageRole.reload', function(event,data) {
		$scope.groupsid = data;
		$scope.findrolegroup(1);
	});
	//接收数据刷新
	eventBusService.subscribe('content.pageGroup.reload', function(event,data) {
		$scope.rolesid = data;
		$scope.findrole($scope.groupt,1);
	});

	// 按钮点击事件-条件查询
	$scope.select = function(){
		$scope.page.current = 1;
		$scope.find();
	}

	// 按钮点击事件-添加用户
	$scope.add = function(fk_role,type){
    	eventBusService.publish("modal.page","modules/content/usermanagement/role/addUser?fk_role="+fk_role+"&type="+type+"&fk_group="+$scope.fk_group+"&fk_unit="+$scope.form.fk_unit);
	}
	
	// 按钮点击事件-添加学生
	$scope.addstudent = function(fk_role,type){
    	eventBusService.publish("modal.page","modules/content/usermanagement/role/addStudent?fk_role="+fk_role+"&type="+type+"&fk_group="+$scope.fk_group+"&fk_unit="+$scope.form.fk_unit);
	}
	
	// 按钮点击事件-移除用户
	$scope.delete = function(){
		var values = [];
		var ids="";
    	$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){ 
    		values.push($(this).val());
    		ids = ids+",'"+$(this).val()+"'";
    	});
    	
    	if(values.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要删除数据。"});
    	}else{
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"条数据？","isAutoClose":false,"confirm":function(){
    			httpService.post(app.urls.role_deleteUserRole,{"ids":ids.replace(',',''),"fk_role":$scope.fk_role},function(data){
    				if("0000" === data.code){
    					$scope.select();
    	    		}else{
    	    			alert(data.msg);
    	    		}
    			});
    		},"cancel":function(){
    			// 用户取消，不做操作
    		}});
    	}
	}
	
	// 按钮点击事件-移除角色
	$scope.deleteroles = function(row){
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除？","isAutoClose":false,"confirm":function(){
    			httpService.post(app.urls.role_selectUserRole,{"id":row,"unitId":$scope.form.unitId},function(data){
    				if("0000" === data.code){
    					if(data.data.length == 0){
    						httpService.post(app.urls.role_delete,{"ids":row},function(data){
    		    				if("0000" === data.code){
    		    					$scope.select();
    		    				}else{
    		    					alert(data.msg);
    		    				}
    		    			});
    						
    						
    					}else{
    						alert("该角色下有用户!");
    					}
    				}else{
    					alert(data.msg);
    				}
    			});
    		},"cancel":function(){
    			// 用户取消，不做操作
    		}});
	}
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAlls = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});